package com.bweab.service.impl.en;


import com.bweab.dao.api.En.DepositTbDaoEnInterface;
import com.bweab.dao.impl.en.DepositTbDaoEnInterfaceImpl;
import com.bweab.pojo.en.DepositTbPageEn;
import com.bweab.service.api.en.DepositTbEnServiceInterface;
import com.bweab.util.JSONStats;
import com.bweab.util.PageList;

import java.util.List;


/**
 * 充值记录页面服务操作的相关实现
 */

public class DepositTbEnServiceInterfaceImpl implements DepositTbEnServiceInterface {

    //创建DaoImpl
    DepositTbDaoEnInterface depositTbDaoEn=new DepositTbDaoEnInterfaceImpl();
    @Override
    public PageList queryDepositInfoByDepositId(int depositId, int page, int pageSize) {
        PageList pageList= new PageList();
         List<DepositTbPageEn> depositTbPageEns = depositTbDaoEn.queryDepositInfoByDepositId(depositId,page,pageSize);
        if (depositTbPageEns.size() > 0) {
            //定义totalPageCount
            int totalPageCount;
            //查询成功
            //取里面的总数
            int totalCount = depositTbPageEns.get(0).getTotalCount();
            //封装返回结果
            //对总页数进行处理
            if(totalCount%pageSize==0){
                totalPageCount=totalCount/pageSize;
            }else {
                totalPageCount=totalCount/pageSize + 1;
            }
            System.out.println(totalPageCount);
            pageList.setPage(page);
            pageList.setPageList(depositTbPageEns);
            pageList.setPageTotal(totalPageCount);
            pageList.setPageSize(pageSize);
            pageList.setMessage(JSONStats.QUERY_SUCCESS.getMessage());
            pageList.setStatusCode(JSONStats.QUERY_SUCCESS.getStatusCode());

        }else {
            //查询失败
            pageList.setMessage(JSONStats.QUERY_ERROR.getMessage());
            pageList.setStatusCode(JSONStats.QUERY_ERROR.getStatusCode());

        }

        return pageList;
    }


    /**
     * 通过寝室id获取充值记录
     *
     * @param page
     * @param pageSize
     * @param dormitoryId 寝室id
     * @return 返回的对象是处理好的响应结果
     */
    @Override
    public PageList queryDepositInfoByDormitoryId(int page, int pageSize, int dormitoryId) {

        //调用DepositEndao
        PageList pageList= new PageList<>();
        //从dao层获取到的数据
        List<DepositTbPageEn> depositTbPageEnList=depositTbDaoEn.queryDepositInfoByDormitoryId(dormitoryId,page,pageSize);
        //如果集合不为空
        if(depositTbPageEnList.size()>0){
            //取总数
            int  counts= depositTbPageEnList.get(0).getTotalCount();

            //对总数进行处理
            int pageTotal;
            if(counts%page==0){
                pageTotal=counts/page;
            }else {
                pageTotal=counts/page + 1;
            }
            //设置页数
            pageList.setPageTotal(pageTotal);
            //设置页面大小
            pageList.setPageSize(pageSize);
            pageList.setPage(page);
            pageList.setPageList(depositTbPageEnList);
            //设置响应结果
            pageList.setStatusCode(JSONStats.QUERY_SUCCESS.getStatusCode());
            pageList.setMessage(JSONStats.QUERY_SUCCESS.getMessage());

            //返回响应结果
        }else {
            //失败
            //设置响应结果
            pageList.setStatusCode(JSONStats.QUERY_ERROR.getStatusCode());
            pageList.setMessage(JSONStats.QUERY_ERROR.getMessage());

        }
        return pageList;
    }


    /**
     * 获取pagelist 并对其进行处理
     * @param userId 用户id
     * @param page      当前页数
     * @param pageSize  页面大小
     * @return 返回结果是一个depositEn的集合
     *
     */
    @Override
    public PageList queryDepositInfoByUserId(int userId, int page, int pageSize) {
        //创建pageList
        PageList pageList = new PageList();

        //调用Dao 获取userID查询的数据集合
        List<DepositTbPageEn> depositTbPageEnList= depositTbDaoEn.queryDepositInfoByUserId(userId,page,pageSize);

        if(depositTbPageEnList.size()>0){
            int totalCount = depositTbPageEnList.get(0).getTotalCount();
            //获取页面总数
            int totalPageCount;
            //获取总数
            if(totalCount%pageSize==0){
                 totalPageCount= totalCount/pageSize;
            }else {
                totalPageCount= totalCount/pageSize +1;
            }
            //设置数据
            pageList.setPageTotal(totalPageCount);
            //设置页面大小
            pageList.setPageSize(pageSize);
            //设置当前页面
            pageList.setPage(page);
            //设置数据
            pageList.setPageList(depositTbPageEnList);
            //设置响应数据
            pageList.setMessage(JSONStats.QUERY_SUCCESS.getMessage());
            pageList.setStatusCode(JSONStats.QUERY_SUCCESS.getStatusCode());

        }else {
            //失败
            //设置响应数据
            pageList.setMessage(JSONStats.QUERY_ERROR.getMessage());
            pageList.setStatusCode(JSONStats.QUERY_ERROR.getStatusCode());

        }
        return pageList;
    }
}
